﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using SmlEngine;
using SmlEngine.Graphics;

namespace SmlEngine.Graphics
{
    /// <summary>
    /// Loads and stores spritesheets.
    /// </summary>
    public static class SpritesheetManager
    {
        private static Dictionary<string, Texture2D> sheets = new Dictionary<string, Texture2D>();

        private static void LoadSheet(string content_path, ContentManager cm)
        {
            if (!sheets.ContainsKey(content_path))
            {
                Texture2D newSheet = cm.Load<Texture2D>(content_path);
                sheets.Add(content_path, newSheet);
            }
        }

        /// <summary>
        /// Returns a texture from a given tilesheet.
        /// </summary>
        /// <param name="content_path">A path to the content or file of the tilesheet.</param>
        /// <param name="sourceRect">The source area of the tile.</param>
        /// <param name="cm">The ContentManager to use.</param>
        /// <returns>The tile.</returns>
        public static Texture2D GetTile(string content_path, Rectangle sourceRect, ContentManager cm)
        {
            // TODO: add range check
            LoadSheet(content_path, cm);
            return sheets[content_path].Crop(sourceRect);
        }
    }
}
